'use strict';

const Controller = require('egg').Controller;
const fs = require("fs");
const path = require("path");

class articleController extends Controller {

    // 图片显示
    async upload() {
        const { ctx } = this;
        const file = ctx.request.files[0];
        const data = {
            "sucuss": 1,
            "pic": {}
        }
        console.log(file);
        fs.copyFileSync(file.filepath, path.dirname(__dirname) + '/public/uploads/' + path.basename(file.filepath))
        data.pic.url = "http://127.0.0.1:81/public/uploads/" + path.basename(file.filepath);
        // data.data.alt = file.filename;
        console.log(data.pic.url);
        // let reslut = {code:1,msg:"添加成功"};
        ctx.body = data;
    }
    // 文章发布
    async publish() {
        const { ctx } = this;
        let res = { code: 1, msg: "上传成功" };
        let data = ctx.request.body;
        console.log(data);
        // 发布人的信息
        // SESSION 会话控制
        data.uid = ctx.session.uid;
        //到数据表里面去验证数据的正确性
        const result = await this.app.mysql.insert('articles', data);
        //学会打印观察数据
        console.log(result);
        if (!result.insertId) {
            res = { code: -1, msg: '添加失败' };
        }

        ctx.body = res;
    }
    // 文章列表
    async list() {
        const { ctx } = this;
        let res = { code: 1, msg: '获取成功' };
        let data = ctx.request.body;
        // console.log(data);
        //到数据表里面去验证数据的正确性
        const sql1 = `SELECT articles.*, users.username,users.avater, users.fans FROM articles, users WHERE articles.status = 1 and articles.uid = users.uid`;
        const result = await this.app.mysql.query(sql1);
        // console.log(result);
        if (!result) {
            res = { code: -1, msg: '获取失败' };
        }
        res.auser = result;

        ctx.body = res;
    }
    // 文章分类
    async sort() {
        const { ctx } = this;
        let res = { code: 1, msg: '获取成功' };
        let data = ctx.request.query;
        // console.log(data);
        //到数据表里面去验证数据的正确性
        const sql1 = `SELECT sort.describe, sort.details, sort.pic FROM sort WHERE cid=${data.cid}`;
        const result = await this.app.mysql.query(sql1);
        // console.log(result);
        if (!result) {
            res = { code: -1, msg: '获取失败' };
        }
        res.sort = result;

        ctx.body = res;
    }

    // 获取草稿
    async issuefalse() {
        const { ctx } = this;
        // SESSION 会话控制
        let uid = ctx.session.uid;
        const results = await this.app.mysql.select('articles', { // 搜索 post 表
            where: { issue: 0, uid }, // WHERE 条件
            columns: ['cid', 'title', "content", "issue", "updatetimes"], // 要查询的表字段
            orders: [['aid', 'desc']], // 排序方式
            limit: 10, // 返回数据量 
        });
        //   => SELECT `author`, `title` FROM `posts`
        //     WHERE `status` = 'draft' AND `author` IN('author1','author2')
        //     ORDER BY `created_at` DESC, `id` DESC LIMIT 0, 10;

        //学会打印观察数据
        console.log(results);
        ctx.body = results;
    }
}

module.exports = articleController;